/*
SPDX-License-Identifier: Apache-2.0
*/

package main

import (
	"fmt"
	"math/rand"
	"strconv"

	"github.com/hyperledger/fabric-contract-api-go/contractapi"
)

// SmartContract provides functions for managing a car
type SmartContract struct {
	contractapi.Contract
}

// InitLedger adds a base set of cars to the ledger
func (s *SmartContract) Put(ctx contractapi.TransactionContextInterface) error {
	i := rand.Int()
	err := ctx.GetStub().PutState("key" + strconv.Itoa(i), []byte("value" + strconv.Itoa(i)))

	if err != nil {
		return fmt.Errorf("Failed to put to world state. %s", err.Error())
	}

	return nil
}


// QueryCar returns the car stored in the world state with given id
func (s *SmartContract) Get(ctx contractapi.TransactionContextInterface) ([]byte, error) {
	i := rand.Int()
	result, err := ctx.GetStub().GetState("key" + strconv.Itoa(i))

	if err != nil {
		return nil, fmt.Errorf("Failed to read from world state. %s", err.Error())
	}

	return result, nil
}

func main() {

	chaincode, err := contractapi.NewChaincode(new(SmartContract))

	if err != nil {
		fmt.Printf("Error create fabcar chaincode: %s", err.Error())
		return
	}

	if err := chaincode.Start(); err != nil {
		fmt.Printf("Error starting fabcar chaincode: %s", err.Error())
	}
}
